/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is "http://semi-frame.googlecode.com/svn/trenk/".
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file StringUtil.cpp
 * @brief Utility of String.
 * @author mornongcloud
 * @date 2011/03/11
 */

#include "semi/util/StringUtil.h"

namespace semi {
namespace util {

unsigned char StringUtil::_ascii[256] = { 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d,
		0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
		0x00, 0x12, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x00, 0x00,
		0x00, 0x00, 0x40, 0x00, 0x7f, 0x7b, 0xe0, 0x6c, 0x50, 0x7d, 0x4d, 0x5d,
		0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5,
		0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, 0x7c, 0xc1,
		0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4,
		0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
		0xe9, 0x4a, 0x5b, 0x5a, 0x6a, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x4f, 0xd0,
		0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41,
		0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54,
		0x55, 0x56, 0x58, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
		0x8a, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
		0x97, 0x98, 0x99, 0x9a, 0x9d, 0x9e, 0x9f, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
		0xa7, 0xa8, 0xa9, 0xaa, 0xac, 0xad, 0xae, 0xaf, 0xba, 0xbb, 0xbc, 0xbd,
		0xbe, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

unsigned char StringUtil::_ebcidc[256] = { 0x00, 0x01, 0x02, 0x03, 0x00, 0x09,
		0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
		0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
		0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a, 0x20, 0xa1,
		0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x5b, 0x2e, 0x3c, 0x28,
		0x2b, 0x7c, 0x26, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0x00, 0xb0, 0x00,
		0x5d, 0x5c, 0x2a, 0x29, 0x3b, 0x00, 0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x23, 0x40, 0x27,
		0x3d, 0x22, 0x00, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
		0xba, 0x00, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4,
		0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0x00, 0x00, 0xca, 0xcb, 0xcc, 0x00, 0x00,
		0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0x00, 0xd6, 0xd7,
		0xd8, 0xd9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45,
		0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7d, 0x4a,
		0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x24, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
		0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

StringUtil::StringUtil() {
	// Auto-generated constructor stub

}

StringUtil::~StringUtil() {
	// Auto-generated destructor stub
}

Strings StringUtil::split(std::string str, std::string sep) {

	Strings ret;
	Strings::iterator ite = ret.begin();
	for (int i = 0, n; i <= str.length(); i = n + 1) {

		n = str.find_first_of(sep, i);

		if (n == std::string::npos)
			n = str.length();

		std::string tmp = str.substr(i, n - i);

		ret.insert(ite, tmp);
	}
	return ret;
}

std::string StringUtil::replace(std::string source, std::string from,
		std::string to) {

	std::string ret;
	std::string::size_type pos_b = 0;
	std::string::size_type pos_c = 0;
	std::string::size_type len = from.size();

	while ((pos_c = source.find(from, pos_c)) != std::string::npos) {
		ret.append(source, pos_b, pos_c - pos_b);
		ret.append(to);
		pos_c += len;
		pos_b = pos_c;
	}
	ret.append(source, pos_b, source.size() - pos_b);
	return ret;
}

std::string StringUtil::trim(std::string source) {
	// return StringUtil::replace(source, " ", "");
	std::string ret = "";
	const char* tmp = source.c_str();
	std::string::size_type pos_b = 0;
	for (int i = 0; i < source.length() && ' ' == tmp[i]; i++)
		pos_b++;

	std::string::size_type pos_a = source.length() - pos_b;
	for (int i = source.length(); 0 < i && ' ' == tmp[i - 1]; i--)
		pos_a--;

	ret.append(source, pos_b, pos_a);

	return ret;
}

int StringUtil::compare_string(const void * elem1, const void * elem2) {
	std::string & e1 = *(std::string *) elem1;
	std::string & e2 = *(std::string *) elem2;
	return e1 == e2 ? 0 : (e1 > e2 ? 1 : -1);
}

void StringUtil::qsort_string(std::string vals[], int size) {
	qsort(vals, size, sizeof(vals[0]), compare_string);
}

std::string StringUtil::EbcdicToAscii(char *src, int length) {
	char dest[length];
	//    char* destP = &dest[0];
	for (int i = 0; i < length; i++) {
		dest[i] = (unsigned char) StringUtil::_ebcidc[(unsigned char) src[i]];
	}
	return std::string(dest, length);
}

std::string StringUtil::AsciiToEbcdic(char *src, int length) {
	char dest[length];
	//    char* destP = &dest[0];
	for (int i = 0; i < length; i++) {
		dest[i] = (unsigned char) StringUtil::_ascii[(unsigned char) src[i]];
	}
	return std::string(dest, length);
}

std::string StringUtil::BcdToAscii(char *bcd, int figure) {
	char str[figure];
	for (int i = 0; i < figure; i++) {
		if (0 <= bcd[i] && bcd[i] <= 9) {
			str[i] = bcd[i] + 0x30;
		} else {
			str[i] = ' ';
		}
	}
	return std::string(str, figure);
}

std::string StringUtil::AsciiToBcd(char *ascii, int figure) {
	char str[figure];
	for (int i = 0; i < figure; i++) {
		if ('0' <= ascii[i] && ascii[i] <= '9') {
			str[i] = ascii[i] - 0x30;
		} else {
			str[i] = ' ';
		}
	}
	return std::string(str, figure);
}

// static
long StringUtil::toLong(std::string str, int def) {
	return StringUtil::toLong((char*) str.c_str(), str.length(), def);
}

// static
long StringUtil::toLong(char* str, int size, int def) {

	int base = 10; // need else?

	char *endpnt;
	char tmp[size + 1];
	memset(tmp, 0, sizeof(tmp));
	strncpy(tmp, str, size);
	long ret = strtol(tmp, &endpnt, base);
	if (*endpnt != EXIT_SUCCESS || (ret == LONG_MAX && EXIT_SUCCESS == ERANGE)) {
		if (LONG_MIN == def) {
			std::cout << "fail! strtol end point:" << endpnt << std::endl;
			throw std::logic_error(
					"argument illegal on toLong of semi.util.StringUtil( "
							+ std::string(tmp) + " ) end at " + std::string(
							endpnt));
		}
		return def;
	}
	return ret;
}

}
}
